<?php
class LogBaseController extends BaseController{
	public function __construct(){
		parent::__construct();
	}
	
	public function exportLog(){
		$exportType = $_GET['exportType'];
		
		$manager = new LogManager();
		$returnList = $manager->pageShow("export");
		
		if(!count($returnList['tableValue'])){
			Handler_tool::alertAndClose("没有可导出的记录！");
		}
		
		$memberInfo = $this->user;
		
		if($exportType == "excel"){
			$downType = 0;
			$fileType = "xls";
		}else{
			$downType = 1;
			$fileType = "txt";
		}
		$downloadFile = $returnList['table']."_".date("YmdHis").".".$fileType;
		
		LogManager::writeDownLog($memberInfo['id'], $memberInfo['username'], $downType, $returnList['table'], $downloadFile);
		
		$echoStr = "";
		foreach ($returnList['tableTitle'] as $value){
			$echoStr .= $value."\t";
		}
		$echoStr .= "\n";
		foreach ($returnList['tableValue'] as $value){
			foreach ($value['value'] as $value1){
				$echoStr .= $value1."\t";
			}
			$echoStr .= "\n";
		}
		
		$downloadFile = urlencode($downloadFile);
		$echoStr = iconv("UTF-8", "GB2312", $echoStr);
		if($exportType == "excel"){
			header("Content-Type:application/vnd.ms-excel;");
			header("Content-Disposition:filename={$downloadFile}");
		}else{
			header("Content-Type: application/force-download");
			header("Content-Disposition:attachment;filename=".$downloadFile);
		}
		
		echo $echoStr;
	}
	
	public function deleteLog(){
		$manager = new LogManager();
		$returnList = $manager->pageShow("delete");
		
		if(!count($returnList['tableValue'])){
			Handler_tool::alertAndClose("没有可删除的记录！");
		}
		
		$memberInfo = $this->user;
		
		$downType = 0;
		$fileType = "xls";
		$downloadFile = $returnList['table']."_".date("YmdHis").".".$fileType;
		
		LogManager::writeDownLog($memberInfo['id'], $memberInfo['username'], $downType, $returnList['table'], $downloadFile);
		
		$echoStr = "";
		foreach ($returnList['tableTitle'] as $value){
			$echoStr .= $value."\t";
		}
		$echoStr .= "\n";
		foreach ($returnList['tableValue'] as $value){
			foreach ($value['value'] as $value1){
				$echoStr .= $value1."\t";
			}
			$echoStr .= "\n";
		}
		
		$downloadFile = urlencode($downloadFile);
		$echoStr = iconv("UTF-8", "GB2312", $echoStr);
		header("Content-Type:application/vnd.ms-excel;");
		header("Content-Disposition:filename={$downloadFile}");
		
		echo $echoStr;
	}
	
	public function printLog(){
		$manager = new LogManager();
		$returnList = $manager->pageShow("print");
		
		if(!count($returnList['tableValue'])){
			Handler_tool::alertAndClose("没有可打印的记录！");
		}
		
		$this->tpl->assign('pageName', $returnList["pageName"]);
		$this->tpl->assign('tableTitle', $returnList["tableTitle"]);
		$this->tpl->assign('tableValue', $returnList["tableValue"]);
		
		$this->tpl->display("log/printLog.html");
	}
}
